/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.BorderLayout;
import org.aswing.JButton;
import org.aswing.JPanel;
import org.aswing.JSlider;
import org.aswing.JToggleButton;
import org.aswing.SoftBoxLayout;

import fever.log.FeverDebug;
import fever.utils.Stringifier;

import fvaswing.components.reader.abstract.FvReaderEvent;
import fvaswing.components.reader.video.FvVideoDocument;
import fvaswing.components.reader.video.FvVideoDocumentListener;
import fvaswing.components.reader.video.icons.FvMuteIcon;
import fvaswing.components.reader.video.icons.FvPauseIcon;
import fvaswing.components.reader.video.icons.FvPlayIcon;
import fvaswing.components.reader.video.icons.FvStopIcon;

/**
 * Basic control bar for {@link fvaswing.components.reader.FvVideoPlayer} 
 * player.
 * 
 * <p>Customize your own video control bar using 
 * {@link fvaswing.components.reader.FvVideoPlayer#setVideoUIControl()} 
 * method.
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.reader.video.FvBasicVideoUI extends JPanel 
	implements FvVideoDocumentListener
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _document : FvVideoDocument;
	
	private var _playButton : JButton;
	private var _stopButton : JButton;
	private var _muteButton : JToggleButton;
	private var _videoSlider : JSlider;
	private var _lockVideoEventFlow : Boolean;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function FvBasicVideoUI()
	{
		super( new BorderLayout( 5,5 ) );
		
		_lockVideoEventFlow = false;
		
		setVisible( false );
		
		_configureUI();
	}	
	
	/**
	 * Inits UI with passed-in {@link fvaswing.components.reader.video.FvVideoDocument} 
	 * video document.
	 */
	public function onMetaData( event : FvReaderEvent ) : Void
	{
		FeverDebug.WARN( event.getType() );
		
		_document = FvVideoDocument( event.getDocumentManager() );
		
		_videoSlider.setMaximum( _document.getDocument().getDuration() );
		_videoSlider.setEnabled( true );
		onVideoPlayChange( event );
		
		setVisible( true );
	}
	
	/**
	 * Update progression bar with current time
	 */
	public function onVideoPlayHeadChange( event : FvReaderEvent ) : Void
	{
		if( !_lockVideoEventFlow )
			_videoSlider.setValue( FvVideoDocument( event.getDocumentManager() ).getPlayHeadTime() );
	}
	
	/**
	 * Triggered when an error occured during video loading process.
	 * 
	 * <p>Thus video is not available, hides the control bar.
	 */
	public function onErrorDocument( event : FvReaderEvent ) : Void
	{
		setVisible( false );
	}
	
	/**
	 * Triggered when user change "play/pause" state of video.
	 */
	public function onVideoPlayChange( event : FvReaderEvent ) : Void
	{
		if( FvVideoDocument( event.getDocumentManager() ).isPlaying() )
			_playButton.setIcon( new FvPauseIcon() );
		else _playButton.setIcon( new FvPlayIcon() );
		
		_videoSlider.setEnabled( true );
	}
	
	/**
	 * Triggered when user stop video.
	 */
	public function onVideoStop( event : FvReaderEvent ) : Void
	{
		_videoSlider.setEnabled( false );
		
		_playButton.setIcon( new FvPlayIcon() );
	}
	
	/**
	 * Triggered when user change "mute" state of video.
	 */
	public function onVideoMute( event : FvReaderEvent ) : Void
	{
		_muteButton.setSelected( FvVideoDocument( event.getDocumentManager() ).isMute() );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _configureUI() : Void
	{
		_playButton = new JButton( new FvPlayIcon() );
		_playButton.addActionListener( _handlePlayPause, this );
		_playButton.setPreferredWidth( 50 );
		
		_stopButton = new JButton( new FvStopIcon() );
		_stopButton.addActionListener( _handleStop, this );
		_stopButton.setPreferredWidth( 50 );
		
		_videoSlider = new JSlider( JSlider.HORIZONTAL, 0, 100 );
		_videoSlider.addEventListener( JSlider.ON_PRESS, _lockVideoEvent, this );
		_videoSlider.addEventListener( JSlider.ON_RELEASE, _handleSeek, this );
		_videoSlider.addEventListener( JSlider.ON_RELEASEOUTSIDE, _handleSeek, this );
		
		_muteButton = new JToggleButton( new FvMuteIcon() );
		_muteButton.addActionListener( _handleMute, this );
		_muteButton.setPreferredWidth( 50 );
		
		var p : JPanel = new JPanel( new SoftBoxLayout( SoftBoxLayout.X_AXIS, 5 ) );
		p.append( _playButton );
		p.append( _stopButton );
		
		append( p, BorderLayout.WEST );
		append( _videoSlider, BorderLayout.CENTER );
		append( _muteButton, BorderLayout.EAST );
	}
	
	private function _lockVideoEvent() : Void
	{
		_lockVideoEventFlow = true;	
	}
	
	private function _unlockVideoEvent() : Void
	{
		_lockVideoEventFlow = false;
	}
	
	private function _handleSeek( source : JSlider ) : Void
	{
		_document.seek( source.getValue() );
		_unlockVideoEvent();
	}
	
	private function _handlePlayPause( source : JButton ) : Void
	{
		_document.toggle();	
	}
	
	private function _handleMute( source : JToggleButton ) : Void
	{
		_document.mute();	
	}
	
	private function _handleStop( source : JButton ) : Void
	{
		_document.stop();	
	}
}